// 引入接口文件
import {
    save,
    edit,
    queryInfo,
    ondelete,
    loadData,
    selectProject,
    selectReason,
    offlineCommission,
    queryWxyxCommissionBByMainId,
    verifyCommissionTokenFun
    } from './commissionsListReasonServ'
    
    // 对象和JSON字符串互转
    import { parse } from 'qs'
    // 弹出消息框
    import { Modal,message } from 'antd'
    // const confirm = Modal.confirm; 
    // 日期处理对象
    // import moment from 'moment'
    /*isEmpty 对象是否为空
     *isArray 对象是否数组
     *cloneDeep 对象深拷贝
     */
    import { isEmpty, isArray, cloneDeep, merge, pick,set,get } from 'lodash'
    // 引入路由对象
    import { hashHistory } from 'dva/router'
    import config from 'config/config'
    import $ from 'jquery';
    // 初始默认状态
    const defultState = {
      id: '',
      loading: false,
      uploading: false,
      invalidLoading: false,
      keys: 0,
      createFaymentMethodFlag: true,
      actionType:'check',
      biz: '',
      biz1: {},
      detail: {
        "approvenote": "",
        "approvetime": "",
        "bpmStatus": "",
        "c3Vbillid": "",
        "c3Vbillstatus": "",
        "createBy": "",
        "createDate": "",
        "createName": "",
        "dmaketime": "",
        "enddate": "",
        "id": "",
        "jsdx": "",
        "nccommissionid": "",
        "nccorp": "",
        "nccorpname": "",
        "pkProject": "",
        "projectName": "",
        "remark": "",
        "statdate": "",
        "summoneySh": 0,
        "summoneySq": 0,
        "sysCompanyCode": "",
        "sysOrgCode": "",
        "updateBy": "",
        "updateDate": "",
        "updateName": "",
        "usercode": "",
        "username": "",
        "vbillno": "",
        "vbillstatus": "",
        "wxyxCommissionBList": [],
        "zjVbillstatus": "",
        "zyx1": "",
        "zyx10": "",
        "zyx11": "",
        "zyx12": "",
        "zyx13": "",
        "zyx14": "",
        "zyx15": "",
        "zyx16": "",
        "zyx17": "",
        "zyx2": "",
        "zyx3": "",
        "zyx4": "",
        "zyx5": "",
        "zyx6": "",
        "zyx7": "",
        "zyx8": "",
        "zyx9": ""
      },    
      remark: '',
      tel:'',      
      selectedRowKeys: [],
      selectedRows: [],
      selectedRowKeys1: [],
      selectedRows1:[],
      projectModalObj: {
        visible: false
      },
      payModalObj:{
        visible: false,
        paymentMethodCode:"",
        planCommissionDate:"",
        paymentMethodName: ""
      },
      accountingModalObj:{
        visible: false,
        isTransferProperty:'',
        projectId:'',
        projectName:''
      },
      reasonModalObj:{
        visible: false,
        Reason:''
      },
      reasonList:[],
      suitableDataList: [],
      projectList:[],
      paymentMethodList:[
        {
          paymentMethodCode:'SD040201',
          paymentMethodName:'一次性付款'
        },
        {
          paymentMethodCode:'SD040202',
          paymentMethodName:'员工分期付款'
        },
        {
          paymentMethodCode:'SD040203',
          paymentMethodName:'商业贷款'
        },
        {
          paymentMethodCode:'SD040204',
          paymentMethodName:'公积金按揭'
        },
        {
          paymentMethodCode:'SD040205',
          paymentMethodName:'混合贷款'
        },
        {
          paymentMethodCode:'SD040207',
          paymentMethodName:'非员工全款分期付款'
        },
        {
          paymentMethodCode:'SD040208',
          paymentMethodName:'非员工首付分期付款'
        }
      ],
    }
    // 声明module
    const tmpModule = {
      // namespace: 'secondKillingSetting',
      // 默认状态
      state: cloneDeep(defultState),
      // 入口函数(先执行)
      subscriptions: {
        setup({ dispatch, history }) {
          history.listen(({ pathname, query }) => {
            // 页面路由匹配时执行
            if ('' + tmpModule.pathname === '' + pathname) {
              query.id=query.id+''
              dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态
              if (query.actionType == 'edit'|| query.actionType == 'check') {
                dispatch({type: 'queryInfo',payload: {actionType: query.actionType,id: query.id, } })
              } else {
                dispatch({type: 'updateStore',payload: {actionType: query.actionType,id: query.id, } })
              }
            } // end if
          }) // end listen
        },
      },
      
      // 方法列表
      effects: {
        *queryInfo({payload}, {put, call, select}) {
          if(yield call(verifyCommissionTokenFun,{})){
            //查询
            let {detail} = yield select(state => state[tmpModule.namespace])
            let params = {
              id: payload.id
            }
            const {result, success} = yield call(queryInfo,params)
            if(success){
              detail = result
              yield put({type: 'updateStore',payload: {detail}})
              yield put({type: 'queryWxyxCommissionBByMainId',payload: {id: payload.id}})
            }
          }
          
        },
        *queryWxyxCommissionBByMainId({payload}, {put, call, select}) {
          if(yield call(verifyCommissionTokenFun,{})){
            //查询明细
            let {detail} = yield select(state => state[tmpModule.namespace])
            let params = {
              id: payload.id
            }
            const {result, success} = yield call(queryWxyxCommissionBByMainId,params)
            if(success){
              detail.wxyxCommissionBList = result
              yield put({type: 'updateStore',payload: {detail}})
            }
          }
        },
        *offlineCommission({payload}, {put, call, select}) {
          if(yield call(verifyCommissionTokenFun,{})){
            //线下结佣
            let {detail,selectedRows1,uploading} = yield select(state => state[tmpModule.namespace])
            let arr = []
            for(var item of selectedRows1){
              arr.push(item.commissionId)
            }
            let params = {
              commissionIds: arr.join(','),
              // paymentMethodCode: payload.id,
              // paymentMethodName: payload.id,
              // planCommissionDate: payload.id,
            }
            yield put({type: 'updateStore', payload: {uploading:true}})
            const {result, success,code} = yield call(offlineCommission,params)
            if(code){
              yield put({type: 'updateStore', payload: {uploading:false}})
            }
            if(success){
              message.success('操作成功')
              for(var it of selectedRows1){
                detail.wxyxCommissionBList = detail.wxyxCommissionBList.filter(item => item.key != it.key)
              }
              yield put({type: 'updateStore', payload: {detail}})
            }
          }
        },
        *ondelete({payload},{put,select,call}){
          if(yield call(verifyCommissionTokenFun,{})){
            //作废
            let {selectedRows1,reasonModalObj,uploading,detail} = yield select(state => state[tmpModule.namespace])
            let list = selectedRows1.map((item,index)=>{
              return {
                transactionId: item.transactionid,
                projectId: item.pkProject,
                houseName: item.qyfh,
                ztcustomerId: item.customerId
              }
            })
            for(var pt of list){
              pt.invalidReason = reasonModalObj.Reason
            }
            yield put({type: 'updateStore', payload: {uploading:true}})
            const { success, code, data } = yield call(ondelete, list)
            if(code){
              yield put({type: 'updateStore', payload: {uploading:false}})
            }
            if(success){
              message.success('作废成功')
              for(var it of selectedRows1){
                detail.wxyxCommissionBList = detail.wxyxCommissionBList.filter(item => item.key != it.key)
              }
              yield put({type: 'updateStore', payload: {detail}})
            }
          }
        },
        *save({payload},{put,select,call}){
          if(yield call(verifyCommissionTokenFun,{})){
            //保存
            let {detail,createFaymentMethodFlag,uploading} = yield select(state => state[tmpModule.namespace])
            if(!createFaymentMethodFlag){
              message.warn('请先生成佣金明细')
              return false
            }
            let params = detail
            yield put({type: 'updateStore', payload: {uploading:true}})
            if(!params.id){
              //不包含ID为录入
              const { success, msg, data,code } = yield call(save, {...params})
              if(code){
                yield put({type: 'updateStore', payload: {uploading:false}})
              }
              if(success){
                message.success('保存成功')
                hashHistory.push({
                  pathname: '/home/commissions/commissionsList',
                  query: {}
                })
              }
            }else{
              //包含Id为编辑
              const { success, msg, data,code } = yield call(edit, {...params})
              if(code){
                yield put({type: 'updateStore', payload: {uploading:false}})
              }
              if(success){
                message.success('修改成功')
                hashHistory.push({
                  pathname: '/home/commissions/commissionsList',
                  query: {}
                })
              }
            }
          }
        },
        *loadData({payload}, {put, call, select}) {
          if(yield call(verifyCommissionTokenFun,{})){
            //生成客户明细
            let {detail,uploading} = yield select(state => state[tmpModule.namespace])
            let params = {
              enddate: detail.enddate,
              pkProject: detail.pkProject,
              zyx2: detail.zyx2,
            }
            yield put({type: 'updateStore', payload: {uploading:true}})
            const {result, success,code} = yield call(loadData,params)
            if(code){
              yield put({type: 'updateStore', payload: {uploading:false}})
            }
            if(success){
              let wxyxCommissionBList = result.wxyxCommissionBList
              for(var i=0;i<wxyxCommissionBList.length;i++){
                  wxyxCommissionBList[i].keyId = i
              }
              detail = result
              detail.wxyxCommissionBList = wxyxCommissionBList
              yield put({
                type: 'updateStore',
                payload: { detail}
              })
            }
          }
        },
        *selectProject({payload}, {put, call, select}) {
          if(yield call(verifyCommissionTokenFun,{})){
            let {projectList,accountingModalObj} = yield select(state => state[tmpModule.namespace])
            let params = {
                projectName:accountingModalObj.projectName
            }
            const {success,result} = yield call(selectProject,params)
            if(success){
              projectList = result
              yield put({type: 'updateStore',payload: {projectList}})
            }
          }
        },
        *selectReason({payload}, {put, call, select}) {
          if(yield call(verifyCommissionTokenFun,{})){
            let {reasonModalObj,detail} = yield select(state => state[tmpModule.namespace])
            let params = {
                mobile:detail.tel,
                projectId:detail.pkProject
            }
            const {success,result} = yield call(selectReason,params)
            if(success){
                reasonModalObj.remark = result;
              yield put({type: 'updateStore',payload: {reasonModalObj}})
            }
          }
        },
        *selectSubject({payload}, {put, call, select}) {
            let {detail,accountingModalObj} = yield select(state => state[tmpModule.namespace])
            let params = {
                isTransferProperty:accountingModalObj.isTransferProperty,
                projectId:accountingModalObj.projectId
            }
            const {success,result} = yield call(selectSubject,params)
            if(success){
                detail.zyx2 = result.accEntityCode
                detail.zyx3 = result.accEntityName
                yield put({type: 'updateStore',payload: {detail}})
            }
        },
        *selectedKeysChange ({payload}, {put, select, call}) {
          let {selectedRowKeys,selectedRows} = yield select(state => state[tmpModule.namespace])
          selectedRowKeys=payload.selectedRowKeys
          selectedRows=payload.selectedRows
          yield put({
            type: 'updateStore',
            payload: {
              selectedRowKeys,selectedRows
            }
          })
        },
        *selectedKeysChange1 ({payload}, {put, select, call}) {
            let {selectedRowKeys1,selectedRows1} = yield select(state => state[tmpModule.namespace])
            selectedRowKeys1=payload.selectedRowKeys
            selectedRows1=payload.selectedRows
            yield put({
              type: 'updateStore',
              payload: {
                selectedRowKeys1,selectedRows1
              }
            })
        },
      },
      // 存入状态机
      reducers: {
        updateStore(preState, action) {
          return Object.assign({}, preState, action.payload)
        },
      }
    }
    
    export default tmpModule
    